package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.exception.DataNotFoundException;
import com.itheima.common.exception.DeletionNotAllowedException;
import com.itheima.common.exception.DuplicateDataException;
import com.itheima.mapper.CheckGroupCheckItemMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;
    @Autowired
    private CheckGroupCheckItemMapper checkGroupCheckItemMapper;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckItem> page = checkItemMapper.pageQuery(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 添加检查项
     * @param checkItem
     */
    @Override
    public void addCheckItem(CheckItem checkItem) {
        // 检查项目编码是否重复
        CheckItem codeTest = new CheckItem();
        codeTest.setCode(checkItem.getCode());
        List<CheckItem> items = checkItemMapper.select(codeTest);
        if (!items.isEmpty()){
            throw new DuplicateDataException(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        // 项目名称是否重复
        CheckItem nameTest = new CheckItem();
        nameTest.setName(checkItem.getName());
        items = checkItemMapper.select(nameTest);
        if (!items.isEmpty()){
            throw new DuplicateDataException(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        // 添加检查项
        checkItemMapper.insert(checkItem);
    }

    /**
     * 根据id删除检查项
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        // 检查检查项是否被关联
        Integer count = checkGroupCheckItemMapper.countByCheckItemId(id);
        if (count > 0){
            throw new DeletionNotAllowedException(MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        // 删除检查项
        checkItemMapper.deleteById(id);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public CheckItem finById(Integer id) {
        // 封装查询条件到CheckItem对象
        CheckItem checkItem = new CheckItem();
        checkItem.setId(id);
        // 查询
        List<CheckItem> checkItems = checkItemMapper.select(checkItem);
        if (checkItems == null || checkItems.isEmpty()){
            // 如果未查询到数据，抛出异常
            throw new DataNotFoundException(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return checkItems.get(0);
    }

    /**
     * 编辑检查项
     * @param checkItem
     */
    @Override
    public void editCheckItem(CheckItem checkItem) {
        checkItemMapper.update(checkItem);
    }

    /**
     * 查询所有检查项
     * @return
     */
    @Override
    public List<CheckItem> findAll() {
        // 查询所有检查项 --> 查询条件为null即可
        return checkItemMapper.select(null);
    }

    /**
     * 根据检查组id查询关联的检查项id
     * @param checkgroupId
     * @return
     */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkgroupId) {
        return checkGroupCheckItemMapper.selectByCheckGroupId(checkgroupId);
    }
}